package com.gaoch.simweather.tools

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.view.View
import android.widget.Toast
import com.gaoch.simweather.MyApplication
import com.google.android.material.snackbar.Snackbar
fun SharedPreferences.open(block: SharedPreferences.Editor.()->Unit){
    val editor=edit()
    editor.block()
    editor.apply()
}

inline fun <reified T>startActivity(context: Context,block: Intent.()->Unit){
    val intent= Intent(context,T::class.java)
    intent.block()
    context.startActivity(intent)
}

inline fun <reified T>startActivity(context: Context){
    val intent= Intent(context,T::class.java)
    context.startActivity(intent)
}


//顶层函数+扩展函数
fun String.showToast(duration:Int=Toast.LENGTH_SHORT){
    Toast.makeText(MyApplication.context,this,duration).show()
}
fun Int.showToast(duration:Int=Toast.LENGTH_SHORT){
    Toast.makeText(MyApplication.context,this,duration).show()
}

//顶层函数+扩展函数+高阶函数
fun View.showSnackBar(text:String,actionText:String?=null, duration: Int=Snackbar.LENGTH_SHORT,block:(()->Unit)?=null){
    val snackbar=Snackbar.make(this,text,duration)
    if(actionText!=null&&block!=null){
        snackbar.setAction(actionText){
            block
        }
    }
    snackbar.show()

}
fun View.showSnackBar(resId:Int, actionText:String?=null, duration: Int=Snackbar.LENGTH_SHORT,block:(()->Unit)?=null){
    val snackbar=Snackbar.make(this,resId,duration)
    if(actionText!=null&&block!=null){
        snackbar.setAction(actionText){
            block
        }
    }
    snackbar.show()
}

